//
//  CheckInViewController.m
//  Spa
//
//  Created by PangPond on 14/1/2556.
//  Copyright (c) พ.ศ. 2556 innova. All rights reserved.
//

#import "CheckInViewController.h"
#import "Customer.h"
#import "AddCustomerViewController.h"
#import "DetailCheckInViewController.h"
@interface CheckInViewController ()

@end

@implementation CheckInViewController

@synthesize checkInCell = _checkInCell;
@synthesize customer = _customer;
@synthesize tableView = _tableView;
@synthesize databaseUtility =_databaseUtility;
@synthesize keyDetailFirstName;
@synthesize keyDetailLastName;
@synthesize keySelect;
@synthesize btnSelect;
@synthesize btnDetail;
@synthesize keyReload;
@synthesize btnShow;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        self.title = @"CheckIn";
    }
    return self;
}
-(void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    
    if (keyReload == @"1") {
        [self loadView];
        [self viewDidLoad];
    }
}

- (BOOL) hidesBottomBarWhenPushed {
    return YES;
}
- (void)viewDidLoad
{
    [super viewDidLoad];
   // UIBarButtonItem * editCustomer = [[UIBarButtonItem alloc] initWithTitle:@"Add" style:UIBarButtonItemStylePlain target:self action:@selector(AddCustomer:)];
   // self.navigationItem.rightBarButtonItem = editCustomer;
    
    self.hidesBottomBarWhenPushed = YES;
    
     btnSelect.enabled = NO;
     //btnShow.enabled = NO;
    
    self.databaseUtility = [[DatabaseUtility alloc]initWithDatabaseName:@"Database"];
    self.customer = [self.databaseUtility fetchedResultsWithClass:@"Customer"withSortDescriptorString:@"firstname"];
    [self.tableView reloadData];
    
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - UITableViewDataSource


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [self.customer count];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    //////
    static NSString *CellIdentifier = @"CheckInCell";
    
    CheckInCell *cell = (CheckInCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        
        [[NSBundle mainBundle] loadNibNamed:@"CheckInCell" owner:self options:nil];
        
        cell = self.checkInCell;
        self.checkInCell = nil;
    }
    
    //config Cell

    
    Customer * customers = [self.customer objectAtIndex:indexPath.row];

    cell.checkInLabel.text = [NSString stringWithFormat:@"%@  %@",customers.firstname,customers.lastname];
    cell.accessoryType = UITableViewCellAccessoryNone;
    return cell;
}

#pragma mark - UITableViewDelegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    Customer * customers = [self.customer objectAtIndex:indexPath.row];
    keyDetailFirstName = customers.firstname;
    keyDetailLastName = customers.lastname;
    
    //btnShow.enabled = YES;
    btnSelect.enabled = YES;
   
    
    keyReload =@"0";
    
   // [btnDetail addTarget:self action:@selector(btnDetail:) forControlEvents:UIControlEventTouchUpInside];
}


//funtion
-(IBAction)btnDetail:(id)sender{

        AddCustomerViewController *addCustomer = [[AddCustomerViewController alloc] initWithNibName:@"AddCustomerViewController" bundle:nil];
    
        NSIndexPath *indexPath =[self.tableView indexPathForCell:(UITableViewCell *)[[sender superview] superview]];
        NSUInteger *row = indexPath.row;
        Customer * customers = [self.customer objectAtIndex:row];
    
        addCustomer.keyCustomerFirstname = customers.firstname;
        keyReload =@"0";
        addCustomer.keyIf = @"1";
        [self presentViewController:addCustomer animated:YES completion:nil];
    

}


-(IBAction)AddCustomer:(id)sender{
    AddCustomerViewController *addCustomer = [[AddCustomerViewController alloc] initWithNibName:@"AddCustomerViewController" bundle:nil];
    addCustomer.keyIf = @"2";
    keyReload =@"1";
    [self presentViewController:addCustomer animated:YES completion:nil];
    
}
-(IBAction)Select:(id)sender{

    DetailCheckInViewController *detailCheckInViewController = [[DetailCheckInViewController alloc] initWithNibName:@"DetailCheckInViewController" bundle:nil];
    
    detailCheckInViewController.keyCustomer = [NSString stringWithFormat:@"%@ %@",keyDetailFirstName,keyDetailLastName];
    [self presentViewController:detailCheckInViewController animated:YES completion:nil];
}

@end
